/**
 * Created by admin on 2018/3/9.
 */
;(function($){
    var dataSets=[];//数据集合
    //数据去重
    $.dealRepeatData=function(obj){
        if(obj==""){
            dataSets=[];
        }else{
            if(dataSets.length==0){
                dataSets.push(obj)
            }else{
                dataSets.map(function(val,index,input){
                    if(val.questionHistoryId==obj.questionHistoryId){
                        input.splice(index,1,obj);
                    }else{
                        input.push(obj)
                    }
                });
                var hash = {};
                dataSets = dataSets.reduce(function(item, next) {
                    hash[next.questionHistoryId] ? '' : hash[next.questionHistoryId] = true && item.push(next);
                    return item
                }, []);
            }
        }
        return dataSets;
    },
        //把数据存到localStorage里
        $.setLocalData=function(courseId,phaseWorkId,val){
            return localStorage.setItem(courseId+'_'+phaseWorkId,JSON.stringify(val));
        },
        //从localStorage里取数据
        $.getLocalData=function(courseId,phaseWorkId){
            return localStorage.getItem(courseId+'_'+phaseWorkId);
        },
        //从localStorage里把作业数据移除
        $.removeLocalData=function(courseId,phaseWorkId){
            return localStorage.removeItem(courseId+'_'+phaseWorkId);
        },
        //统计题的类型
        $.statisticsQuestionType=function(paperData){
            var questType = [];
            $.each(paperData, function (i, data) {
                questType.push(data.questionType);
            });
            questType = questType.unique3();//统计所有的题目类型
            return questType;
        },
        //显示大图
        $.bigIMG=function(){
            $(".timu").on("click", "img", function () {
                $(this).toggleClass("bigImg","smallImg");
            });
        },
        //单选，多选，判断选择效果
        $.effectOfRadio=function(){
            $(".timu li").click(function () {
                var Indexid, answerid;
                if ($(this).hasClass("danxuanTi") || $(this).hasClass("panduanTi")) {
                    $(this).addClass("select").siblings().removeClass("select");
                    Indexid = $(this).parent().parent().attr("data-indexId");
                } else if ($(this).hasClass("duoxuanTi")) {
                    $(this).toggleClass("select");
                    Indexid = $(this).parent().parent().attr("data-indexId");
                }

                var flag = true;
                var $m = $(this).parent("ol").children();
                $m.each(function () {
                    if ($(this).hasClass("select")) {
                        flag = false;
                    }
                });

                if (flag) {
                    $(".thirdLeiAnswer ol li").each(function () {
                        answerid = $(this).attr("data-answerid");
                        if (Indexid == answerid) {
                            $(this).removeClass("select");
                            return false;
                        }
                    });
                } else {
                    $(".thirdLeiAnswer ol li").each(function () {
                        answerid = $(this).attr("data-answerid");
                        if (Indexid == answerid) {
                            $(this).addClass("select");
                        }
                    });
                }
            });
        },
        //答题或取消答案后，答题卡添加或取消选中样式
        $.addSelectClass=function(IndexId){
            $(".thirdLeiAnswer ol li").each(function () {
                var  answerid = $(this).attr("data-answerid");
                if (IndexId == answerid) {
                    $(this).addClass("select");
                    return false;
                }
            });
        },
        $.cancleSelectClass=function(IndexId){
            $(".thirdLeiAnswer ol li").each(function () {
                var  answerid = $(this).attr("data-answerid");
                if (IndexId == answerid) {
                    $(this).removeClass("select");
                    return false;
                }
            });
        },
        //多选题答案结果显示
        $.duoxuanAnswerResult=function(){
            $(".dxt").each(function () {
                var _this = $(this).find("li");
                var arr = _this.parent().attr("data-da");
                var Answer = $(this).attr("data-answer");
                if (Answer != "" && Answer != null) {
                    var ansCheck = JSON.parse(Answer);
                }
                if (arr != "" && arr != null) {
                    arr = JSON.parse(arr);
                }

                for (var j = 0; j < arr.length; j++) {
                    if (ansCheck.indexOf(arr[j]) != -1) {
                        $(this).find('#li' + arr[j]).addClass('correntAns');
                    } else {
                        $(this).find('#li' + arr[j]).addClass('errorAns');
                    }
                }
            });
        },
        //页面滚动
        $.answerCardScroll=function(){
            $(".thirdLeiAnswer ol li").click(function () {
                var answerId = $(this).attr("data-answerid");
                localStorage.setItem("scrollType",answerId);
                $(document).unbind("scroll");
                $(".timu>li").each(function () {
                    var dataIndexId = $(this).attr("data-indexid");
                    var dataType = $(this).attr("data-type");
                    var currentTop = $(this).offset().top;
                    if (dataIndexId === answerId) {
                        $(".timuTypeBox li").each(function () {
                            var timutype = $(this).attr("data-timutype");
                            if (timutype == dataType) {
                                $.scrollTiType(answerId);
                            }
                        });
                        $("body,html").animate({"scrollTop": currentTop - 59 + "px"}, function () {
                            $.scrollTiType();
                            $(document).bind("scroll", function () {
                                $.scrollTiType('');
                            });
                        });
                        return false;
                    }
                });
            });
            $(".timuTypeBox li").click(function () {
                var timuType = $(this).attr("data-timuType");
                localStorage.setItem("scrollType",timuType);
                $(this).addClass("select").siblings().removeClass("select");
                $(document).unbind("scroll");
                $(".timu>li").each(function () {
                    var dataType = $(this).attr("data-Type");
                    var currentTop = $(this).offset().top;
                    if (dataType === timuType) {
                        $("body,html").animate({"scrollTop": currentTop - 59 + "px"},500, function () {
                            $.scrollTiType();
                            $(document).bind("scroll", function () {
                                $.scrollTiType("");
                            });
                        });
                        return false;
                    }
                });
            })
        },

        $.scrollTiType=function(){
            var examHeight = $(".examBasicInfo").innerHeight() + $(".timuTypeBox").innerHeight();
            var answerCardLeft = $(".examContent").offset().left + $(".timuAndAnswer").innerWidth() + 10;
            if ($(document).scrollTop() > examHeight) {
                $(".timuTypeBox").addClass("examFloat");
                $(".answerCard").addClass("answerCardFloat").css("left", answerCardLeft + "px");
                var scrollType=localStorage.getItem("scrollType");
            } else {
                $(".timuTypeBox").removeClass("examFloat");
                $(".answerCard").removeClass("answerCardFloat");
            }
            if(scrollType){
                setTimeout(function(){
                    $(".d"+scrollType).addClass("select").siblings().removeClass("select");
                    localStorage.setItem("scrollType","");
                },500);
            }else{
                if ($(".timu li").hasClass("question0")) {
                    var danxuanTop = $(".question0").offset().top - 80;
                }
                if ($(".timu li").hasClass("question1")) {
                    var duoxuanTop = $(".question1").offset().top - 80;
                }
                if ($(".timu li").hasClass("question2")) {
                    var panduanTop = $(".question2").offset().top - 80;
                }
                if ($(".timu li").hasClass("question3")) {
                    var tiankongTop = $(".question3").offset().top - 80;
                }
                if ($(".timu li").hasClass("question4")) {
                    var jiandaTop = $(".question4").offset().top - 80;
                }
                if ($(".timu li").hasClass("question5")) {
                    var daimaTop = $(".question5").offset().top - 80;
                }
                if ($(".timu li").hasClass("question6")) {
                    var shicaoTop = $(".question6").offset().top - 80;
                }
                if ($(document).scrollTop() > danxuanTop) {
                    $(".d0").addClass("select").siblings().removeClass("select");
                }
                if ($(document).scrollTop() > duoxuanTop) {
                    $(".d1").addClass("select").siblings().removeClass("select");
                }
                if ($(document).scrollTop() > panduanTop) {
                    $(".d2").addClass("select").siblings().removeClass("select");
                }
                if ($(document).scrollTop() > tiankongTop) {
                    $(".d3").addClass("select").siblings().removeClass("select");
                }
                if ($(document).scrollTop() > jiandaTop) {
                    $(".d4").addClass("select").siblings().removeClass("select");
                }
                if ($(document).scrollTop() > daimaTop) {
                    $(".d5").addClass("select").siblings().removeClass("select");
                }
                if ($(document).scrollTop() > shicaoTop) {
                    $(".d6").addClass("select").siblings().removeClass("select");
                }
            }

        },
        //未答完页面弹框
        $.noCompletePopUp=function(submitAnswer){
            $("body").append(template.compile(popupTemp1)());
            var datiStatus = false;
            var count = [];
            $(".radioAnswer li").each(function () {
                if (!$(this).hasClass("select")) {
                    count.push($(this).attr("data-answerid"));
                    datiStatus = true;//未答完
                }
            });
            if (datiStatus) {
                var str = "";
                for (var i = 0; i < count.length; i++) {
                    if (i < 5) {
                        str += "【" + ++count[i] + "】";
                    } else {
                        str += "…";
                        break;
                    }
                }
                $(".noCompleteHint .noCompleteContent .count").html(str);
                $(".noCompleteHint").css("display", "block");
            } else {
                $(".yidawan").css("display", "block");
            }
            $(".continueAnswer").click(function () {
                $(".noCompleteHint").remove();
            });
            $(".noCompleteHint .noCompleteInnerBox .guanbi").click(function(){
                $(".noCompleteHint").remove();
            });

            $(".noCompleteHint .tijiao").click(function () {
                RequestService("/online/user/isAlive","GET",null,function(data){
                    if(data.success==true){
                        $(".confirm").addClass("disable");
                        submitAnswer();
                    }else{
                        $(".confirm").removeClass("disable");
                        $("#login").modal('show');
                    }
                });
            });
        }
})(jQuery);